package android.crosstime.hes;

import java.io.File;

import edu.android.hes.R;

public class ListItemUtil {
	
	public final static int ICON_DIR 		= R.drawable.icon_folder;
	public final static int ICON_UNKNOWN 	= R.drawable.icon_unknown;
	public final static int ICON_UP_LEVEL	= R.drawable.icon_uplevel;
	
	public final static int TYPE_NORMAL	= 0;
	public final static int TYPE_UPLEVEL = 1;
	public final static int TYPE_UNDEFINE = 2;

	private final static String[] knownFileExt = {
			".txt", 	
			".jpg",		
			".gif", 	
			".bmp"
			
		};
		
	private final static Integer[] IconIds = {
			R.drawable.icon_txt, 
			R.drawable.icon_jpeg, 
			R.drawable.icon_gif, 
			R.drawable.icon_bmp	
		};
		
	public static int getIconIdByType(String fileName){
		
		File file = new File(fileName);
		if(file.isDirectory())
			return ICON_DIR;
		
		int lastDot = fileName.lastIndexOf(".");
		if(lastDot == -1)
			return ICON_UNKNOWN;
		
		String ext = fileName.substring(lastDot).toLowerCase();
		int size = knownFileExt.length;
		for(int i = 0; i < size; i++){
			if(ext.equals(knownFileExt[i]))
				return IconIds[i];
		}
		
		return ICON_UNKNOWN; 
	}
	
	public static int getFileType(String fileName){
		File file = new File(fileName);
		if(file.exists() && file.isDirectory())
			return 0; //directory type set to 0
		
		int lastDot = fileName.lastIndexOf(".");
		if(lastDot == -1 || lastDot+1 >= fileName.length())
			return 999; //biggest value set to unknown type
		return fileName.toLowerCase().charAt(lastDot+1);
	}
}
